# maps of the producers included in the RCT #

# load data and collapse number of producers at the municipality level #

  data <- readstata13::read.dta13("output/data.dta") %>% filter(time == 1) %>% select(mun, CTRL, CAP, CAP_ATEG) %>%
          rename(CD_GEOCMU = mun) %>% mutate(CD_GEOCMU = as.character(CD_GEOCMU)) %>% group_by(CD_GEOCMU) %>%
          summarise(CTRL = sum(CTRL), CAP = sum(CAP), CAP_ATEG = sum(CAP_ATEG)) %>% mutate(Total = CTRL + CAP + CAP_ATEG) %>%
          rename(T0 = CTRL, T1 = CAP, T2 = CAP_ATEG) %>% left_join(st_read("input/shapes/BRMUE250GC_SIR.shp")) %>% st_as_sf()

# load map and merge #  
  
  shape <- st_read("input/shapes/BRUFE250GC_SIR.shp") %>% left_join(read_excel("input/aux/Tabela Estados IBGE.xlsx", col_types = c("text", "text")))

# maps #
  
  # all observations #
  
  map1 =  tm_shape(shape) +
            tm_grid(n.x = 4, n.y = 4, col = "#646464", lwd = 0.1) + 
            tm_text("UF", size = .5) + tm_borders(lwd = 0.7) + 
            tm_shape(data) + tm_bubbles("Total", col = "#e41a1c") +
            tm_layout(main.title = "Panel A: Total", fontface = "bold", fontfamily ="sans", 
                      main.title.size = 1, main.title.position = "center", 
                      legend.title.size = 1, legend.position = c(0.05,0.05),
                      frame = T, frame.lwd = 0.5)

  # controls #
  
  map2 = tm_shape(shape) +
            tm_grid(n.x = 4, n.y = 4, col = "#646464", lwd = 0.1) + 
            tm_text("UF", size = .5) + tm_borders(lwd = 0.7) + 
            tm_shape(data) + tm_bubbles("T0", col = "#377eb8") +
            tm_layout(main.title = "Panel B: T0", fontface = "bold", fontfamily ="sans", 
                      main.title.size = 1, main.title.position = "center", 
                      legend.title.size = 1, legend.position = c(0.05,0.05),
                      frame = T, frame.lwd = 0.5)

  # training #
  
  map3 = tm_shape(shape) +
            tm_grid(n.x = 4, n.y = 4, col = "#646464", lwd = 0.1) + 
            tm_text("UF", size = .5) + tm_borders(lwd = 0.7) + 
            tm_shape(data) + tm_bubbles("T1", col = "#4daf4a") +
            tm_layout(main.title = "Panel C: T1", fontface = "bold", fontfamily ="sans", 
                      main.title.size = 1, main.title.position = "center", 
                      legend.title.size = 1, legend.position = c(0.05,0.05),
                      frame = T, frame.lwd = 0.5)

  # training + technical assistance
  
  map4 = tm_shape(shape) +
            tm_grid(n.x = 4, n.y = 4, col = "#646464", lwd = 0.1) + 
            tm_text("UF", size = .5) + tm_borders(lwd = 0.7) + 
            tm_shape(data) + tm_bubbles("T2", col = "#984ea3") +
            tm_layout(main.title = "Panel D: T2", fontface = "bold", fontfamily ="sans", 
                      main.title.size = 1, main.title.position = "center", 
                      legend.title.size = 1, legend.position = c(0.05,0.05),
                      frame = T, frame.lwd = 0.5)
  
  # save #
  
  tmap_save(tmap_arrange(map1, map2, map3, map4, nrow = 2), filename = "figures/figureS1.png", 
            width = 10, height = 8)
